// pages/operation/followUpManage/addFollowRecord/index.js
import {
  FollowUpModel
} from "../../../../api/api-work/followUpModel.js"
import {
  timestampToTime
} from "../../../../utils/index.js"
const followUpModel = new FollowUpModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentId: "", // xueshengid
    isShowMethod: false,
    followMethodData: [], // 跟进方式
    isShowPhase: false,
    followPhaseData: [], // 跟进阶段
    stateName: "", // 跟进方式内容
    phaseName: "", // 跟进阶段内容
    state: "", // 跟进方式
    phase: "", // 跟进阶段
    isShowTime: false, // 
    contactTime: "", // 跟进时间
    contactStampTime: new Date().getTime(), // 跟进时间
    minDate: new Date(1990, 0, 1).getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      studentId: options.studentId
    })
    // 获取跟进方式
    await this.getFollowMethods()
    // 获取跟进阶段
    await this.getFollowPhase()
  },

  // 获取跟进方式
  async getFollowMethods() {
    followUpModel.getFollowMethodsApi().then(res => {
      console.log("res: ", res)
      let followM = res;
      followM = followM.filter(item => item.dictValue !== '5');
      this.setData({
        followMethodData: followM
      })
    })
  },

  // 获取跟进阶段
  async getFollowPhase() {
    followUpModel.getFollowPhaseApi().then(res => {
      let followP = res;
      followP = followP.filter(item => item.dictValue !== '10');
      this.setData({
        followPhaseData: followP
      })
    })
  },

  showMethod() {
    this.setData({
      isShowMethod: true
    })
  },

  changeMethod(e) {
    const item = e.currentTarget.dataset.query;
    this.setData({
      isShowMethod: false,
      state: item.dictValue,
      stateName: item.dictLabel
    })
  },

  onCloseMethod() {
    this.setData({
      isShowMethod: false
    })
  },

  showPhase() {
    this.setData({
      isShowPhase: true
    })
  },

  changePhase(e) {
    const item = e.currentTarget.dataset.query;
    this.setData({
      isShowPhase: false,
      phase: item.dictValue,
      phaseName: item.dictLabel
    })
  },

  onClosePhase() {
    this.setData({
      isShowPhase: false
    })
  },

  showTime() {
    this.setData({
      isShowTime: true
    })
  },

  selectTime(e) {
    this.setData({
      contactStampTime: e.detail,
      contactTime: timestampToTime(e.detail),
      isShowTime: false
    })
  },

  onCloseTime() {
    this.setData({
      isShowTime: false
    })
  },

  // 完成
  finishFollow() {
    followUpModel.saveFollowApi({
      studentId: this.data.studentId,
      state: this.data.state,
      phase: this.data.phase,
      contactTime: timestampToTime(new Date().getTime()),
      type: "1"
    }).then(res => {
      wx.navigateBack()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})